<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>02-图片上传前的预览</title>
  <style>
    .box {
      width: 300px;
      height: 300px;
      margin:50px auto;
      border: 1px solid #000;
    }
  </style>
</head>
<body>
  <input type="file" id="avatar">
  <div class="box">
    <img src="" alt="">
  </div>
  <script>
    // 1. 获取页面中的标签
    const avatar = document.getElementById('avatar')
    const boxImg = document.querySelector('.box img')
    // 2. 给事件源注册事件
    avatar.onchange = function(event){
      // console.log(123)
      // console.dir(this)
      // console.log(this.files)
      // console.log(this.files[0])
      const imgUrl = URL.createObjectURL(this.files[0])
      console.log(imgUrl)
      boxImg.src = imgUrl
    }
  </script>
</body>
</html>